// Michael Depuy
// depuym@onid.orst.edu
// CS344-400
// Homework 4 

#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/msg.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <assert.h>
#include <strings.h>


#define MAX_TEXT 1024


 struct mymsg{
   long mtype;
   char mtext[MAX_TEXT];
 };

int main(int argc,char **argv){

  int flags = 0, msqid, opt, perms, num_keyflags;
  long lkey;
  key_t key;
  struct mymsg msg;
  int num_forks = 0;
  int pipefd[2];
  char *filename;
  num_keyflags = 0;
  flags = 0;

  /* Setup message queue */
  while((opt = getopt(argc,argv,"cxn:f:k:p")) != -1){
    switch(opt){

    case 'n':
      num_forks = atoi(optarg);
      if(num_forks > 50 || num_forks < 0){
	printf("Invalid input for '-n': must be between 1 & 50\n");
	_exit(EXIT_FAILURE);
	}
      break;

    case 'f':
      filename = optarg;
      break;

    default:
      printf("No arguments provided. Please restart program.\n");
      _exit(0);
    }

  }
  
  if(num_forks == 0)
    num_forks = 1;

  if (num_keyflags != 1){
    printf("Exactly 1 of '-k', '-f', or '-p' can be used. Exiting...\n");
    _exit(0);
  }

  msqid = msgget(key, flags | S_IRUSR | S_IWUSR);
  assert(msqid != -1);

  printf("message queue created.\n");
  printf("flags = %d \t numkeyflags = %d \t key = %d \t num_forks = %d\n ",flags,num_keyflags,key,num_forks);
  printf("deleting message queue...\n");

  /* Create 'n' forks */

  if((pipe(pipefd)) == -1)
    _exit(EXIT_FAILURE);
  
  


  /* Delete the message queue */
  msgctl(msqid, IPC_RMID, NULL);
  _exit(EXIT_SUCCESS);




  return 0;
}
